<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleExport()">
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <a-button type="default" @click="handleImport()" v-auth="'bas:person:edit'">
          <Icon icon="ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button>
        <!-- <a-button type="primary" @click="handleForm({})" v-auth="'bas:person:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button> -->
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ code: record.code, op: 'edit' })">
          {{ record.code }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
    <FormAuthUser @register="registerAuthUserDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsBasPersonList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { basPersonDelete, basPersonListData } from '/@/api/bas/person/basPerson';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';
  import FormAuthUser from './formAuthUser.vue';

  const { t } = useI18n('bas.person.basPerson');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('业务员管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('业务员名称'),
        field: 'name',
        component: 'Input',
      },
      // {
      //   label: t('关联用户'),
      //   field: 'refUser.userCode',
      //   component: 'Input',
      // },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('业务员编码'),
      dataIndex: 'code',
      key: 'a.code',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('业务员名称'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 230,
      align: 'left',
    },
    // {
    //   title: t('关联用户'),
    //   dataIndex: 'refUser.userName',
    //   key: 'a.user_code',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑业务员'),
        onClick: handleForm.bind(this, { code: record.code, op: 'edit' }),
        auth: 'bas:person:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除业务员'),
        popConfirm: {
          title: t('是否确认删除业务员'),
          confirm: handleDelete.bind(this, { code: record.code }),
        },
        auth: 'bas:person:edit',
      },
      {
        icon: 'ant-design:user-outlined',
        title: t('分配客户'),
        onClick: handleFormAuthUser.bind(this, { code: record.code }),
        auth: 'bas:person:edit',
      },
    ],
  };

  const [registerAuthUserDrawer, { openDrawer: openAuthUserDrawer }] = useDrawer();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: basPersonListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    //actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/bas/person/exportData',
      target: '_self',
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  async function handleDelete(record: Recordable) {
    const res = await basPersonDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }

  // 业务员分配客户
  function handleFormAuthUser(record: Recordable) {
    openAuthUserDrawer(true, record);
  }
</script>
